/*
 * Creater: Nguyen Ngoc An
 * Date: 24/6/2014
 * File name: myscript.js
 */
(function() {

    //#region "khach hang"
    /*
     * Su kien an nut edit thong tin khach hang
     */
    jQuery("button[action='editKhachHang']").click(function() {
        jQuery('#myModal').modal('show');
        var idKH = jQuery(this).attr("value");

        jQuery.ajax({
            url: "/customer?do=edit&id=" + idKH,
            type: "GET",
            success: function(html)
            {
                jQuery('#modalContent').html(html);
                jQuery("#btnUpdateKhachHang").click(function() {
                    var name = jQuery("#txtname").val();
                    var phone = jQuery("#txtphone").val();
                    var gender = "";
                    if (jQuery("chkgendernam").checked)
                    {
                        gender = "1";
                    }
                    else
                    {
                        gender = "2";
                    }
                    jQuery.ajax({
                        url: "/customer?do=update&id=" + idKH + "&name=" + name + "&phone=" + phone + "&gender=" + gender,
                        type: "GET",
                        success: function(html) {
                            jQuery('#myModal').modal('hide');
                            jQuery("#tr" + idKH).fadeOut(300);
                            jQuery("#tr" + idKH).html(html);
                            jQuery("#tr" + idKH).toggle(300);
                        },
                        error: function() {
                            //Thong bao loi
                            jQuery('#myModal').modal('hide');
                        }
                    });
                });
            },
            error: function() {
                //Thong bao loi
                jQuery('#myModal').modal('hide');
            }
        });
    });

    /*
     * Su kien an nut delete khach hang
     */
    jQuery("button[action='deleteKhachHang']").click(function() {
        var idKH = jQuery(this).attr("value");

        jQuery.ajax({
            url: "/customer?do=delete&id=" + idKH,
            type: "GET",
            success: function(html) {
                jQuery("#tr" + idKH).fadeOut(300);
                jQuery("#tr" + idKH).html("");
            }
        });
    });
    //#endregion "khach hang"

    /*
     * *************************************************************************
     */

    //#region "Danh muc mon an"
    /*
     * Su kien an nut edit danh muc
     */
    jQuery("button[action='editDanhMuc']").click(function() {
        var idDM = jQuery(this).attr("value");
        jQuery('#myModal').modal('show');
        jQuery.ajax({
            url: "/category?do=edit&id=" + idDM,
            type: "GET",
            success: function(html)
            {
                jQuery('#modalContent').html(html);
                jQuery("#btnUpdateDanhMuc").click(function() {
                    var name = jQuery("#txtname").val();
                    jQuery.ajax({
                        url: "/category?do=update&id=" + idDM + "&name=" + name,
                        type: "GET",
                        success: function(html) {
                            jQuery('#myModal').modal('hide');
                            jQuery("#tr" + idDM).fadeOut(300);
                            jQuery("#tr" + idDM).html(html);
                            jQuery("#tr" + idDM).toggle(300);
                        },
                        error: function() {
                            //Thong bao loi
                            jQuery('#myModal').modal('hide');
                        }
                    });
                });
            },
            error: function() {
                //Thong bao loi
                jQuery('#myModal').modal('hide');
            }
        });
    });

    /*
     * Su kien an nut delete danh muc
     */
    jQuery("button[action='deleteDanhMuc']").click(function() {
        var idDM = jQuery(this).attr("value");
        jQuery.ajax({
            url: "/category?do=delete&id=" + idDM,
            type: "GET",
            success: function(html) {
                jQuery("#tr" + idDM).fadeOut(300);
                jQuery("#tr" + idDM).html("");
            }
        });
    });
    //#endregion "Danh muc mon an"

    //#region "hoa don"
    /*
     * Su kien an nut edit hoa don
     */
//    jQuery("button[action='editHoaDon']").click(function() {
//        jQuery('#myModal').modal('show');
//        jQuery.ajax({
//            url: "Ajax/ajax_hoadon.html",
//            type: "GET",
//            success: function(html)
//            {
//                jQuery('#modalContent').html(html);
//            }
//        });
//    });

    /*
     * Su kien an nut delete hoa don
     */
    jQuery("button[action='deleteHoaDon']").click(function() {
        var idHD = jQuery(this).attr("value");
        jQuery.ajax({
            url: "/bill?do=delete&id=" + idHD,
            type: "GET",
            success: function(html) {
                jQuery("#tr" + idHD).fadeOut(300);
                jQuery("#tr" + idHD).html("");
            }
        });

    });

    /*
     * Chi tiet hoa don
     */
    jQuery("button[action='editKhachHang']").click(function() {
        jQuery('#myModal').modal('show');
        var idKH = jQuery(this).attr("value");

        jQuery.ajax({
            url: "/customer?do=edit&id=" + idKH,
            type: "GET",
            success: function(html)
            {
                jQuery('#modalContent').html(html);
                jQuery("#btnUpdateKhachHang").click(function() {
                    var name = jQuery("#txtname").val();
                    var phone = jQuery("#txtphone").val();
                    var gender = "";
                    if (jQuery("chkgendernam").checked)
                    {
                        gender = "1";
                    }
                    else
                    {
                        gender = "2";
                    }
                    jQuery.ajax({
                        url: "/customer?do=update&id=" + idKH + "&name=" + name + "&phone=" + phone + "&gender=" + gender,
                        type: "GET",
                        success: function(html) {
                            jQuery('#myModal').modal('hide');
                            jQuery("#tr" + idKH).fadeOut(300);
                            jQuery("#tr" + idKH).html(html);
                            jQuery("#tr" + idKH).toggle(300);
                        },
                        error: function() {
                            //Thong bao loi
                            jQuery('#myModal').modal('hide');
                        }
                    });
                });
            },
            error: function() {
                //Thong bao loi
                jQuery('#myModal').modal('hide');
            }
        });
    });

    /*
     * Su kien an nut tim kiem id khach hang
     
     */
    jQuery("button[action='chiTietHoaDon']").click(function() {
        var idHD = jQuery(this).attr("value");

        jQuery.ajax({
            url: "/bill?do=searchidbycustomer&id=" + idKH,
            type: "GET",
            success: function(html)
            {
                var value = $.parseJSON(html);
                console.log(value);
                $('#idcustomeraftersearch').val(value.name);
                $('#phone').val(value.phone);
                // enable button next
                $("#bnt-finish").attr("disabled", false);
                $('#idcustomer-hidden').val(value.id);

            }
        });
    });

    //#endregion "hoa don"
    //Tim kiem khac hang
    jQuery("button[action='searchCustomerbyID']").click(function() {
        var idKH = jQuery('#idcustomer').attr('value');
        jQuery.ajax({
            url: "/bill?do=searchidbycustomer&id=" + idKH,
            type: "GET",    
            success: function(html)
            {
                var value = $.parseJSON(html);
                console.log(value);
                $('#idcustomeraftersearch').val(value.name);
                $('#phone').val(value.phone);
                // enable button next
                $("#bnt-finish").attr("disabled", false);
                $('#idcustomer-hidden').val(value.id);

            }
        });
    });


    //#region "mon an"
    /*
     * Su kien an nut edit mon an
     */
    jQuery("button[action='editMonAn']").click(function() {
        jQuery('#myModal').modal('show');
        var idMA = jQuery(this).attr("value");
        jQuery.ajax({
            url: "/food?do=edit&id=" + idMA,
            type: "GET",
            success: function(html)
            {
                jQuery('#modalContent').html(html);
                jQuery("#btnUpdateMonAn").click(function() {
                    var name = jQuery("#txtname").val();
                    var price = jQuery("#txtprice").val();
                    var catalog = document.getElementById("selectCatalog").value;

                    jQuery.ajax({
                        url: "/food?do=update&id=" + idMA + "&name=" + name + "&price=" + price + "&catalog=" + catalog,
                        type: "GET",
                        success: function(html) {
                            jQuery('#myModal').modal('hide');
                            jQuery("#tr" + idMA).fadeOut(300);
                            jQuery("#tr" + idMA).html(html);
                            jQuery("#tr" + idMA).toggle(300);
                        },
                        error: function() {
                            //Thong bao loi
                            jQuery('#myModal').modal('hide');
                        }
                    });
                });
            },
            error: function() {
                //Thong bao loi
                jQuery('#myModal').modal('hide');
            }
        });
    });

    /*
     * Su kien an nut delete mon an
     */
    jQuery("button[action='deleteMonAn']").click(function() {
        var idMA = jQuery(this).attr("value");
        jQuery.ajax({
            url: "/food?do=delete&id=" + idMA,
            type: "GET",
            success: function(html) {
                jQuery("#tr" + idMA).fadeOut(300);
                jQuery("#tr" + idMA).html("");
            }
        });
    });
    //#endregion "mon an"

    //#region "windown"
//        jQuery.ajax({
//            url: "Ajax/ajax_menu.html",
//            type: "GET",
//            success: function(html) {
//                jQuery("#ajaxMenu").html(html);
//            }
//        });
    //#endregion "windown"

}(window));
